package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.Teachplan;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 课程计划前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/teachplan")
public class TeachplanController {

    @Autowired
    private TeachplanService  teachplanService;

    //课程计划列表-树形结构
    @GetMapping("/{courseId}/tree-nodes")
    public ResponseResult<TeachplanVo> treeNodes(@PathVariable("courseId") Long courseId) {
        return teachplanService.treeNodes(courseId);
    }

    //保存或者更新课程计划
    @PostMapping
    public ResponseResult saveOrUpdate(@RequestBody TeachplanDto dto) {
        return teachplanService.saveOrUpdate(dto);
    }

    //根据id删除课程计划
    @DeleteMapping("/{id}")
    public ResponseResult deleteById(@PathVariable("id") Long id) {
        return teachplanService.deleteById(id);
    }

    //课程计划绑定媒资
    @PostMapping("/media/association")
    public ResponseResult associateMedia(@RequestBody BindTeachplanMediaDto dto) {
        return teachplanService.associateMedia(dto);
    }

    //根据id查询课程计划
    @GetMapping("/{teachplanId}")
    public ResponseResult<TeachplanVo> findPlanById(@PathVariable("teachplanId") Long teachplanId) {
        Teachplan teachplan = teachplanService.getById(teachplanId);
        TeachplanVo vo = BeanHelper.copyProperties(teachplan, TeachplanVo.class);
        return ResponseResult.okResult(vo);
    }
}
